package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.oa.entity.VeFileFilesEntity;
import org.edu.modules.oa.entity.VeOaDocumentEntity;
import org.edu.modules.oa.entity.VeOaHuishouzhanEntity;
import org.edu.modules.oa.mapper.IVeFileFilesMapper;
import org.edu.modules.oa.mapper.IVeOaHuishouzhanMapper;
import org.edu.modules.oa.mapper.VeOaDocumentMapper;
import org.edu.modules.oa.service.IVeOaHuishouzhanService;
import org.edu.modules.oa.service.IVeOaPersonDocumentService;
import org.edu.modules.oa.vo.VeOaDocumentVo;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class VeOaHuishouzhanServiceImpl extends ServiceImpl<IVeOaHuishouzhanMapper, VeOaHuishouzhanEntity> implements IVeOaHuishouzhanService {

    @Resource
    private VeOaDocumentMapper iVeOaDocumentMapper;
    @Resource
    private IVeOaHuishouzhanMapper iVeOaHuishouzhanMapper;
    @Resource
    private IVeFileFilesMapper iVeFileFilesMapper;

    //自己的接口
    @Resource
    private IVeOaHuishouzhanService iVeOaHuishouzhanService;
    //事务管理器
    @Resource(name="transactionManager")
    private DataSourceTransactionManager transactionManager;


    //逻辑删除（单/多）
    // 最后删自己
    @Override
    public Integer deleteTrueDocument(Long id) {
        //获取redis存的用户值
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Integer result = 0;
        //查出下级id
        QueryWrapper<VeOaHuishouzhanEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("IS_DELETED",1);
        queryWrapper.eq("PARENT_ID",id);
        List<VeOaHuishouzhanEntity> sublist = iVeOaHuishouzhanMapper.selectList(queryWrapper);
        for (int i=0;i<sublist.size();i++){
            //是文件时
            if (sublist.get(i).getIdType() == 1){
                sublist.get(i).setIsDeleted(2);
                iVeOaHuishouzhanMapper.updateById(sublist.get(i));
            }
            //是文件夹时
            if (sublist.get(i).getIdType() == 0){
                //调用自己
                iVeOaHuishouzhanService.deleteTrueDocument(sublist.get(i).getId());
            }
        }
        //删除自己
        VeOaHuishouzhanEntity myeEntity = iVeOaHuishouzhanMapper.selectById(id);
        myeEntity.setIsDeleted(2);
        result = iVeOaHuishouzhanMapper.updateById(myeEntity);

        return result;
    }

    // 根据父级ID查询      查询条件--名称，文档编号，上传人，上传时间  (删除字段)
    // 进入下一级          where PARENT_ID = 选中的文件夹的ID  文件不可进入
    @Override
    public HashMap<String, Object> selectDocument(VeOaHuishouzhanEntity veOaHuishouzhanEntity, Integer startPage, Integer pageSize) {
        // 调用mybatisplus的分页
        IPage<VeOaHuishouzhanEntity> page=new Page<>(startPage,pageSize);
        //拼接查询条件
        QueryWrapper<VeOaHuishouzhanEntity> queryWrapper = new QueryWrapper<>();
        if (veOaHuishouzhanEntity.getDocName() != null && !veOaHuishouzhanEntity.getDocName().equals("")){
            queryWrapper.like("DOC_NAME",veOaHuishouzhanEntity.getDocName());
        }
        if (veOaHuishouzhanEntity.getDocCode() != null && !veOaHuishouzhanEntity.getDocCode().equals("")){
            queryWrapper.like("DOC_CODE",veOaHuishouzhanEntity.getDocCode());
        }
        if (veOaHuishouzhanEntity.getCreateUserName() != null && !veOaHuishouzhanEntity.getCreateUserName().equals("")){
            queryWrapper.like("Create_UserName",veOaHuishouzhanEntity.getCreateUserName());
        }
        if (veOaHuishouzhanEntity.getCreateTime() != null && !veOaHuishouzhanEntity.getCreateTime().equals("")){
            //queryWrapper.apply("date_format(CREATE_TIME, 'yyyy-MM-dd') = {0}",veOaHuishouzhanEntity.getCreateTime());
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(veOaHuishouzhanEntity.getCreateTime());
            calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
            Date nextDay=calendar.getTime(); //这个时间就是日期往后推一天的结果
            queryWrapper.between("CREATE_TIME",veOaHuishouzhanEntity.getCreateTime(),nextDay);
        }
        //父级ID
        if (veOaHuishouzhanEntity.getParentId() != null){
            queryWrapper.eq("PARENT_ID",veOaHuishouzhanEntity.getParentId());
        }
        //删除字段
        if (veOaHuishouzhanEntity.getIsDeleted() != null){
            queryWrapper.eq("IS_DELETED",veOaHuishouzhanEntity.getIsDeleted());
        }
        //文档类型
        if (veOaHuishouzhanEntity.getDocType() != null && !veOaHuishouzhanEntity.getDocType().equals("")){
            queryWrapper.eq("DOC_TYPE",veOaHuishouzhanEntity.getDocType());
        }
        //是否共享:0是,1否
        if (veOaHuishouzhanEntity.getIsShare() != null){
            queryWrapper.eq("IS_SHARE",veOaHuishouzhanEntity.getIsShare());
        }

        queryWrapper.orderByDesc("CREATE_TIME");
        //条件+分页查询
        page = iVeOaHuishouzhanMapper.selectPage(page,queryWrapper);
        List<VeOaHuishouzhanEntity> entityList = page.getRecords();
        // for循环把需要用的字段加到 导出集合里面去  其中通过每个fileId查附件信息
        List<VeOaDocumentVo> voList = new ArrayList<>();
        for (VeOaHuishouzhanEntity en:entityList){
            if (en.getFileId() != null){
                //文件id
                veOaHuishouzhanEntity.setFileId(en.getFileId());
            }
            //用fileid查附件信息
            QueryWrapper<VeFileFilesEntity> fileWrapper = new QueryWrapper<>();
            fileWrapper.eq("id",en.getFileId());
            VeFileFilesEntity filesEntity = iVeFileFilesMapper.selectOne(fileWrapper);

            VeOaDocumentVo vo = new VeOaDocumentVo();
            //序号
            vo.setId(en.getId());
            //名称
            vo.setDocName(en.getDocName());
            //文档编号
            vo.setDocCode(en.getDocCode());
            //父级id
            vo.setParentId(en.getParentId());

            //发送时间（时间类型转换成字符串）
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            vo.setCreateTime(formatter.format(en.getCreateTime()));
            //上传人id name
            vo.setCreateUserId(en.getCreateUserId());
            vo.setCreateUserName(en.getCreateUserName());
            vo.setIsDeleted(en.getIsDeleted());
            //IDtype  判断是文件夹/文件
            if (en.getIdType() == 0){
                vo.setIdType("文件夹");
            }else {
                vo.setIdType("文件");
            }
            //备注
            vo.setBeizhu(en.getBeizhu());

            //是否共享
            vo.setIsShare(en.getIsShare());
            //可查看人员id
            vo.setSeeUserIdList(en.getSeeUserIdList());
            //可查看人员
            vo.setSeeUserNameList(en.getSeeUserNameList());
            //可添加人员id
            vo.setAddUserIdList(en.getAddUserIdList());
            //可添加人员
            vo.setAddUserNameList(en.getAddUserNameList());
            //可修改人员id
            vo.setUpdateUserIdList(en.getUpdateUserIdList());
            //可修改人员
            vo.setUpdateUserNameList(en.getUpdateUserNameList());
            //可删除人员id
            vo.setDeleteUserIdList(en.getDeleteUserIdList());
            //可删除人员
            vo.setDeleteUserNameList(en.getDeleteUserNameList());
            if (filesEntity != null){
                //文件大小
                vo.setSize( (filesEntity.getSize() / 1024) );
                //文件类型
                vo.setExt(filesEntity.getExt());
                //文件存储url
                vo.setSourceFile(filesEntity.getSourceFile());
            }
            voList.add(vo);
        }
        //查询表格总行数
        int count = iVeOaHuishouzhanMapper.selectCount(queryWrapper);
        //把查出来的东西存到map里面去
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",voList);
        map.put("count",count);
        return map;
    }

    // 返回上一级          where ID =  当前父级ID   where PARENT_ID= 搜出父级ID  （搜出）返回到父级ID为0时不可返回
    @Override
    public HashMap<String, Object> backToUpLevel(VeOaHuishouzhanEntity veOaHuishouzhanEntity, Integer startPage, Integer pageSize, Integer isDelete, String docType) {
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<VeOaHuishouzhanEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID",veOaHuishouzhanEntity.getParentId());
        //搜出上级的父级ID
        VeOaHuishouzhanEntity parentDocumentEntity = iVeOaHuishouzhanMapper.selectOne(queryWrapper);
        if (parentDocumentEntity == null){
            //返回空的map
            return map;
        }
        //上级的父级ID有值时，查数据，只用parentID查
        //调用mp的分页
        IPage<VeOaHuishouzhanEntity> page = new Page<>(startPage,pageSize);
        //拼接查询条件
        QueryWrapper<VeOaHuishouzhanEntity> mainWrapper = new QueryWrapper<>();
        if (parentDocumentEntity.getParentId() != null){
            mainWrapper.eq("PARENT_ID",parentDocumentEntity.getParentId());
        }
        //删除字段
        if (parentDocumentEntity.getIsDeleted() != null){
            mainWrapper.eq("IS_DELETED",isDelete);
        }
        //文档类型
        /*if (parentDocumentEntity.getDocType() != null && !parentDocumentEntity.getDocType().equals("")){
            mainWrapper.eq("DOC_TYPE",veOaHuishouzhanEntity.getDocType());
        }
        //是否共享:0是,1否
        if (veOaHuishouzhanEntity.getIsShare() != null){
            mainWrapper.eq("IS_SHARE",veOaHuishouzhanEntity.getIsShare());
        }*/
        mainWrapper.orderByDesc("CREATE_TIME");
        //条件+分页查询
        page = iVeOaHuishouzhanMapper.selectPage(page,mainWrapper);
        List<VeOaHuishouzhanEntity> entityList = page.getRecords();
        // for循环把需要用的字段加到 导出集合里面去  其中通过每个fileId查附件信息
        List<VeOaDocumentVo> voList = new ArrayList<>();
        for (VeOaHuishouzhanEntity en:entityList){
            //用fileid查附件信息
            QueryWrapper<VeFileFilesEntity> fileWrapper = new QueryWrapper<>();
            fileWrapper.eq("id",en.getFileId());
            VeFileFilesEntity filesEntity = iVeFileFilesMapper.selectOne(fileWrapper);

            VeOaDocumentVo vo = new VeOaDocumentVo();
            //序号
            vo.setId(en.getId());
            //名称
            vo.setDocName(en.getDocName());
            //文档编号
            vo.setDocCode(en.getDocCode());
            //父级id
            vo.setParentId(en.getParentId());

            //发送时间（时间类型转换成字符串）
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            vo.setCreateTime(formatter.format(en.getCreateTime()));
            //上传人id name
            vo.setCreateUserId(en.getCreateUserId());
            vo.setCreateUserName(en.getCreateUserName());
            vo.setIsDeleted(en.getIsDeleted());
            //IDtype  判断是文件夹/文件
            if (en.getIdType() == 0){
                vo.setIdType("文件夹");
            }else {
                vo.setIdType("文件");
            }
            //备注
            vo.setBeizhu(en.getBeizhu());

            //是否共享
            vo.setIsShare(en.getIsShare());
            //可查看人员id
            vo.setSeeUserIdList(en.getSeeUserIdList());
            //可查看人员
            vo.setSeeUserNameList(en.getSeeUserNameList());
            //可添加人员id
            vo.setAddUserIdList(en.getAddUserIdList());
            //可添加人员
            vo.setAddUserNameList(en.getAddUserNameList());
            //可修改人员id
            vo.setUpdateUserIdList(en.getUpdateUserIdList());
            //可修改人员
            vo.setUpdateUserNameList(en.getUpdateUserNameList());
            //可删除人员id
            vo.setDeleteUserIdList(en.getDeleteUserIdList());
            //可删除人员
            vo.setDeleteUserNameList(en.getDeleteUserNameList());
            if (filesEntity != null){
                //文件大小
                vo.setSize( (filesEntity.getSize() / 1024) );
                //文件类型
                vo.setExt(filesEntity.getExt());
                //文件存储url
                vo.setSourceFile(filesEntity.getSourceFile());
            }
            voList.add(vo);
        }
        //查询表格总行数
        int count = iVeOaHuishouzhanMapper.selectCount(mainWrapper);
        //把查出来的东西存到map里面去
        map.put("list",voList);
        map.put("count",count);
        return map;
    }

    //导出当前页记录   根据父级ID查询   查询条件--名称，文档编号，上传人，上传时间  (删除字段)
    @Override
    public List<VeOaDocumentVo> export(VeOaHuishouzhanEntity veOaHuishouzhanEntity, Integer startPage, Integer pageSize) {
        // 调用mybatisplus的分页
        IPage<VeOaHuishouzhanEntity> page=new Page<>(startPage,pageSize);
        //拼接查询条件
        QueryWrapper<VeOaHuishouzhanEntity> queryWrapper = new QueryWrapper<>();
        if (veOaHuishouzhanEntity.getDocName() != null && !veOaHuishouzhanEntity.getDocName().equals("")){
            queryWrapper.like("DOC_NAME",veOaHuishouzhanEntity.getDocName());
        }
        if (veOaHuishouzhanEntity.getDocCode() != null && !veOaHuishouzhanEntity.getDocCode().equals("")){
            queryWrapper.like("DOC_CODE",veOaHuishouzhanEntity.getDocCode());
        }
        if (veOaHuishouzhanEntity.getCreateUserName() != null && !veOaHuishouzhanEntity.getCreateUserName().equals("")){
            queryWrapper.like("Create_UserName",veOaHuishouzhanEntity.getCreateUserName());
        }
        if (veOaHuishouzhanEntity.getCreateTime() != null && !veOaHuishouzhanEntity.getCreateTime().equals("")){
            //"date_format(create_date, '%Y-%m-%d') = {0}", date
            //queryWrapper.apply("date_format(CREATE_TIME, 'yyyy-MM-dd') = {0}",veOaHuishouzhanEntity.getCreateTime());
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(veOaHuishouzhanEntity.getCreateTime());
            calendar.add(calendar.DATE,1); //把日期往后增加一天,整数  往后推,负数往前移动
            Date nextDay=calendar.getTime(); //这个时间就是日期往后推一天的结果
            queryWrapper.between("CREATE_TIME",veOaHuishouzhanEntity.getCreateTime(),nextDay);
        }
        //父级ID
        if (veOaHuishouzhanEntity.getParentId() != null){
            queryWrapper.eq("PARENT_ID",veOaHuishouzhanEntity.getParentId());
        }
        //删除字段
        if (veOaHuishouzhanEntity.getIsDeleted() != null){
            queryWrapper.eq("IS_DELETED",veOaHuishouzhanEntity.getIsDeleted());
        }
        //文档类型
        if (veOaHuishouzhanEntity.getDocType() != null && !veOaHuishouzhanEntity.getDocType().equals("")){
            queryWrapper.eq("DOC_TYPE",veOaHuishouzhanEntity.getDocType());
        }
        //是否共享:0是,1否
        if (veOaHuishouzhanEntity.getIsShare() != null){
            queryWrapper.eq("IS_SHARE",veOaHuishouzhanEntity.getIsShare());
        }
        queryWrapper.orderByDesc("CREATE_TIME");
        //queryWrapper.orderByAsc("ID");
        //条件+分页查询
        page = iVeOaHuishouzhanMapper.selectPage(page,queryWrapper);
        List<VeOaHuishouzhanEntity> entityList = page.getRecords();

        // for循环把需要用的字段加到 导出集合里面去  其中通过每个fileId查附件信息
        List<VeOaDocumentVo> voList = new ArrayList<>();
        for (VeOaHuishouzhanEntity en:entityList){
            //用fileid查附件信息
            QueryWrapper<VeFileFilesEntity> fileWrapper = new QueryWrapper<>();
            fileWrapper.eq("id",en.getFileId());
            VeFileFilesEntity filesEntity = iVeFileFilesMapper.selectOne(fileWrapper);

            VeOaDocumentVo vo = new VeOaDocumentVo();
            //序号
            vo.setId(en.getId());
            //名称
            vo.setDocName(en.getDocName());
            //文档编号
            vo.setDocCode(en.getDocCode());
            //父级id
            vo.setParentId(en.getParentId());
            //发送时间（时间类型转换成字符串）
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            vo.setCreateTime(formatter.format(en.getCreateTime()));
            //上传人
            vo.setCreateUserName(en.getCreateUserName());
            //IDtype  判断是文件夹/文件
            if (en.getIdType() == 0){
                vo.setIdType("文件夹");
            }else {
                vo.setIdType("文件");
            }

            //备注
            vo.setBeizhu(en.getBeizhu());
            //文档类型
            vo.setDocType(en.getDocType());
            //是否共享
            /*vo.setIsShare(en.getIsShare());
            //可查看人员id
            vo.setSeeUserIdList(en.getSeeUserIdList());
            //可查看人员
            vo.setSeeUserNameList(en.getSeeUserNameList());
            //可添加人员id
            vo.setAddUserIdList(en.getAddUserIdList());
            //可添加人员
            vo.setAddUserNameList(en.getAddUserNameList());
            //可修改人员id
            vo.setUpdateUserIdList(en.getUpdateUserIdList());
            //可修改人员
            vo.setUpdateUserNameList(en.getUpdateUserNameList());
            //可删除人员id
            vo.setDeleteUserIdList(en.getDeleteUserIdList());
            //可删除人员
            vo.setDeleteUserNameList(en.getDeleteUserNameList());*/
            if (filesEntity != null){
                //文件大小
                vo.setSize( (filesEntity.getSize() / 1024) );
                //文件类型
                vo.setExt(filesEntity.getExt());
                //文件存储url
                vo.setSourceFile(filesEntity.getSourceFile());
            }


            voList.add(vo);
        }

        return voList;
    }

    //恢复文件/文件夹及下级路径的文件/文件夹 --递归
    @Override
    public Integer recoverFileFolder(Long id,Integer parentId) {
        //获取redis存的用户值
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Integer result = 0;
        //查出下级id
        QueryWrapper<VeOaHuishouzhanEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PARENT_ID",id);
        queryWrapper.eq("IS_DELETED",1);
        List<VeOaHuishouzhanEntity> sublist = iVeOaHuishouzhanMapper.selectList(queryWrapper);
        for (int i=0;i<sublist.size();i++){
            //还没在回收站再次删除时
            if (sublist.get(i).getIsDeleted() == 1){
                //是文件时
                if (sublist.get(i).getIdType() == 1){
                    sublist.get(i).setIsDeleted(0);
                    //sublist.get(i).setParentId(parentId);
                    iVeOaHuishouzhanMapper.updateById(sublist.get(i));
                }
                //是文件夹时
                if (sublist.get(i).getIdType() == 0){
                    //调用自己
                    iVeOaHuishouzhanService.recoverFileFolder(sublist.get(i).getId(),parentId);
                }
            }

        }
        //恢复自己
        VeOaHuishouzhanEntity myeEntity = iVeOaHuishouzhanMapper.selectById(id);
        myeEntity.setIsDeleted(0);
        //myeEntity.setParentId(parentId);
        result = iVeOaHuishouzhanMapper.updateById(myeEntity);

        return result;
    }


    //用传递来的id集合   恢复文件/文件夹的判断   where id = parentId,看搜出来的isdelete是否为0，为0没被删除，再看名称，为1就拿这搜出的parentid再找
    //判断上级文件夹有没被删除，有就一直找到没被删除的为止  0为最上级，
    //没被删，再判断是否与该级路径下的文件/文件夹 重名，重名提示用户存在同名文件或文件夹。无就恢复
    //找到可以恢复的id作为要恢复的上级id 外层for
    @Override
    public Long findRecoverIdIsTheirParentId(Long itsid,Integer curPid) {
        Long id = -1L;
        //找可恢复的上级id
        VeOaHuishouzhanEntity upEntity = new VeOaHuishouzhanEntity();
        if (curPid == 0){
            //upEntity = iVeOaHuishouzhanMapper.selectById(curPid);
            upEntity.setParentId(null);
            upEntity.setId(itsid);

        }else {
            upEntity = iVeOaHuishouzhanMapper.selectById(curPid);
        }

        //如果上级的父级ID为0 最顶级
        if (upEntity.getParentId() == null){
            //重名校验
            QueryWrapper<VeOaHuishouzhanEntity> queryWrapper = new QueryWrapper<>();
            VeOaHuishouzhanEntity entity = iVeOaHuishouzhanMapper.selectById(itsid);
            queryWrapper.eq("DOC_NAME",entity.getDocName());
            queryWrapper.eq("IS_DELETED",0);
            queryWrapper.eq("PARENT_ID",0);
            //文档类型
            if (entity.getDocType() != null && !entity.getDocType().equals("")){
                queryWrapper.eq("DOC_TYPE",entity.getDocType());
            }
            //是否共享:0是,1否
            if (entity.getIsShare() != null){
                queryWrapper.eq("IS_SHARE",entity.getIsShare());
            }
            queryWrapper.ne("ID",itsid);
            VeOaHuishouzhanEntity chongfuEntity = iVeOaHuishouzhanMapper.selectOne(queryWrapper);
            if (chongfuEntity != null){
                //重名不可恢复
                id = -1L;
                return id;
            } else {
                //不重名,可恢复
                return 0L;

            }

        }

        if (upEntity.getParentId() == 0){
            //上级没被删除
            if (upEntity.getIsDeleted() == 0){
                //重名校验
                QueryWrapper<VeOaHuishouzhanEntity> queryWrapper = new QueryWrapper<>();
                VeOaHuishouzhanEntity entity = iVeOaHuishouzhanMapper.selectById(itsid);
                queryWrapper.eq("DOC_NAME",entity.getDocName());
                queryWrapper.eq("IS_DELETED",0);
                queryWrapper.eq("PARENT_ID",curPid);
                //文档类型
                if (entity.getDocType() != null && !entity.getDocType().equals("")){
                    queryWrapper.eq("DOC_TYPE",entity.getDocType());
                }
                //是否共享:0是,1否
                if (entity.getIsShare() != null){
                    queryWrapper.eq("IS_SHARE",entity.getIsShare());
                }
                queryWrapper.ne("ID",itsid);
                VeOaHuishouzhanEntity chongfuEntity = iVeOaHuishouzhanMapper.selectOne(queryWrapper);
                if (chongfuEntity != null){
                    //重名不可恢复
                    id = -1L;
                    return id;
                } else {
                    //不重名,可恢复
                    return upEntity.getId();

                }

            } else {
                //再往上找
                id = iVeOaHuishouzhanService.findRecoverIdIsTheirParentId(itsid,upEntity.getParentId());
                return id;
            }
        }else {
            //上级没被删除
            if (upEntity.getIsDeleted() == 0){
                //重名校验
                QueryWrapper<VeOaHuishouzhanEntity> queryWrapper = new QueryWrapper<>();
                VeOaHuishouzhanEntity entity = iVeOaHuishouzhanMapper.selectById(itsid);
                queryWrapper.eq("DOC_NAME",entity.getDocName());
                queryWrapper.eq("IS_DELETED",0);
                queryWrapper.eq("PARENT_ID",curPid);
                //文档类型
                if (entity.getDocType() != null && !entity.getDocType().equals("")){
                    queryWrapper.eq("DOC_TYPE",entity.getDocType());
                }
                //是否共享:0是,1否
                if (entity.getIsShare() != null){
                    queryWrapper.eq("IS_SHARE",entity.getIsShare());
                }
                queryWrapper.ne("ID",itsid);
                VeOaHuishouzhanEntity chongfuEntity = iVeOaHuishouzhanMapper.selectOne(queryWrapper);
                if (chongfuEntity != null){
                    //重名不可恢复
                    id = -1L;
                    return id;
                } else {
                    //不重名,可恢复
                    return upEntity.getId();

                }

            } else {
                //再往上找
                id = iVeOaHuishouzhanService.findRecoverIdIsTheirParentId(itsid,upEntity.getParentId());
                return id;
            }
        }

    }

    //用id查上级id
    @Override
    public VeOaHuishouzhanEntity findParentId(VeOaHuishouzhanEntity veOaHuishouzhanEntity) {
        QueryWrapper<VeOaHuishouzhanEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("IS_DELETED",1);
        queryWrapper.eq("ID",veOaHuishouzhanEntity.getId());
        VeOaHuishouzhanEntity entity = iVeOaHuishouzhanMapper.selectOne(queryWrapper);
        return entity;
    }

    //恢复到能恢复的上级的id --改parentId


    @Override
    public Integer recoverToItsId(Long id, Integer parentId) {
        VeOaHuishouzhanEntity veOaHuishouzhanEntity = new VeOaHuishouzhanEntity();
        veOaHuishouzhanEntity.setId(id);
        veOaHuishouzhanEntity.setParentId(parentId);
        Integer result = iVeOaHuishouzhanMapper.updateById(veOaHuishouzhanEntity);
        return result;
    }
}
